﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
using System.Configuration;

namespace HundSunDBQryFrmClient.Frm
{
    public partial class FormChangePassword : Form
    {
        public WebRsp rsp;
        public FormChangePassword()
        {
            InitializeComponent();
            textBox1.PasswordChar = '*';
            textBox2.PasswordChar = '*';
            textBox3.PasswordChar = '*';
            this.AcceptButton = button1;
            this.CancelButton = button2;
        }

        public static WebRsp show() 
        {
            FormChangePassword frm=new FormChangePassword();
            frm.ShowDialog();
            return frm.rsp;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty( textBox1.Text)||string.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("密码不能为空");
            }else
            if (textBox2.Text!=textBox3.Text)
            {
                MessageBox.Show("确认密码错误");
            }
            else
            {
                using (HttpClient client = new HttpClient())
                {
                    string url = new Uri(new Uri(ConfigurationManager.AppSettings["ServerBaseUrl"]), "/api/ChangePassword").ToString();
                    StringContent content = new StringContent($"oldpassword={textBox1.Text}&newpassword={textBox2.Text}&token={Program.token}");
                    var response = client.PostAsync(url, content).Result;
                    string resstr = response.Content.ReadAsStringAsync().Result;
                    var webrsp = JsonConvert.DeserializeObject<WebRsp>(resstr);
                    if (webrsp != null)
                    {
                        if (webrsp.code == 0)
                        {
                            rsp = webrsp;
                            Close();
                        }
                        else
                        {
                            MessageBox.Show(webrsp.errmsg);
                        }
                    }
                    else
                    {
                        MessageBox.Show("服务器返回错误");
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
